package com.ld.common.util.task;

import java.util.Date;

import com.ld.admin.model.timedtask.SysTimedtaskLogModel;
import com.ld.admin.model.timedtask.SysTimedtaskUserInfo;
import com.ld.shieldsb.common.composition.util.PKUtil;
import com.ld.shieldsb.common.core.util.StringUtils;
import com.ld.shieldsb.common.web.BasicService;
import com.ld.shieldsb.common.web.util.WebUtil;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TimedTaskLogUtil {
    public static final BasicService service = new BasicService();
    private static String serverIp = "127.0.0.1";// 服务器公网IP

    static {
        try {
            serverIp = WebUtil.getServerIp(); // 服务器公网IP
        } catch (Exception e) {
            log.warn("获取服务器公网ip出错", e);
        }
    }

    public static void saveLogbyDB(int type, long taskId, String remarks) {
        saveLogbyDB(type, taskId, remarks, null);
    }

    public static void saveLogbyDB(int type, long taskId, String remarks, SysTimedtaskUserInfo userInfo) {
        saveLogbyDB(type, taskId, remarks, userInfo, true);
    }

    /**
     * 保存日志
     * 
     * @Title saveLogbyDB
     * @author 吕凯
     * @date 2018年6月14日 上午11:21:00
     * @param logType
     * @param logTitle
     * @param request
     *            void
     */
    public static void saveLogbyDB(int type, long taskId, String remarks, SysTimedtaskUserInfo userInfo, Boolean saveDB) {
        Long userId = 0L;
        String userName = null;
        String ip = null;
        if (userInfo != null) {
            userName = userInfo.getUserName();
            ip = userInfo.getIp();
        }
        if (userName == null) {
            userName = "系统操作";
        }
        if (userInfo != null && userInfo.getUserId() == null) {
            userId = userInfo.getUserId();
        }
        SysTimedtaskLogModel manageLogModel = new SysTimedtaskLogModel();
        manageLogModel.setId(PKUtil.nextId());
        manageLogModel.setType(type);
        manageLogModel.setTaskId(taskId);
        manageLogModel.setCreateBy(userId);
        manageLogModel.setCreateName(userName);
        manageLogModel.setExecTime(new Date());
        if (StringUtils.isNotEmpty(ip)) {
            manageLogModel.setServerIp(userInfo.getIp()); // 获取请求ipWebUtil.getIpAddr(request)
        } else {
            manageLogModel.setServerIp(serverIp); // 获取服务器ip
        }
        manageLogModel.setRemarks(remarks);

        if (saveDB) {
            saveLogbyDB(manageLogModel);
        } else {
            log.warn(manageLogModel + ""); // 输出到日志
        }
    }

    /**
     * 保存日志
     * 
     * @Title saveLogbyDB
     * @author 吕凯
     * @date 2018年6月14日 上午11:21:11
     * @param manageLogModel
     *            void
     */
    public static void saveLogbyDB(SysTimedtaskLogModel manageLogModel) {
        if (manageLogModel != null) {
            try {
                service.getBaseDAO().save(manageLogModel);
            } catch (Exception e) {
                log.error("保存日志出错！", e);
            }
        }
    }

}
